package service_handler

import (
	"fmt"
	"peilian-api/app/global/bind"
	"peilian-api/app/global/databases/tables"
	"peilian-api/app/global/errors"
	"peilian-api/app/global/variable"
	"peilian-api/app/model"
	"peilian-api/app/services"
	"peilian-api/app/services/apis_service"
	"peilian-api/app/services/career_service"
	"peilian-api/app/services/interview_service"
	"peilian-api/app/services/report_service"
	"peilian-api/app/services/tiku_service"
	"peilian-api/utils/format"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func GetProfessionList(c *gin.Context) {
	var gpfl model.GetProfessionListReq
	if err := bind.ShouldBinding(c, &gpfl); err != nil {
		format.NewResponseJson(c).Err(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	_, out, err := new(career_service.CareerService).GetProfessionList(c)
	if err != nil {
		format.NewResponseJson(c).Err(err.Code, err.Err.Error())
		return
	}
	format.NewResponseJson(c).SuccessObj(out)
}

func CreateSession(ctx *gin.Context) {
	var sq services.SessionReq
	if err := ctx.ShouldBindJSON(&sq); err != nil {
		format.NewResponseJson(ctx).SuccessObjMsg(30001, err.Error())
		return
	}
	out, err := new(services.PacticeService).CreateSession(ctx, sq)
	if err != nil {
		format.NewResponseJson(ctx).Err(500, err.Error())
		return
	}
	ctx.JSON(200, gin.H{
		"code": 200,
		"data": out,
		"msg":  "OK",
	})
}

// 回答完成
func AskFinished(c *gin.Context) {
	var req model.AskFinishedReq
	if err := bind.ShouldBinding(c, &req); err != nil {
		format.NewResponseJson(c).Err(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	err := new(interview_service.Client).AskFinished(c, req)
	if err != nil {
		format.NewResponseJson(c).Err(500, err.Error())
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": nil,
		"msg":  "OK",
	})
}

func GetSessionQues(c *gin.Context) {
	var gsqr model.GetSessionQuesReq
	if err := bind.ShouldBinding(c, &gsqr); err != nil {
		format.NewResponseJson(c).Err(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	out, err := new(apis_service.ApisService).GetSessionQues(c, gsqr)
	if err != nil {
		format.NewResponseJson(c).Err(err.Code, err.Err.Error())
		return
	}
	format.NewResponseJson(c).SuccessObj(out)
}

// 获取预热视频
func GetPreheadVideo(c *gin.Context) {
	var t model.TypeStrReq
	if err := bind.ShouldBinding(c, &t); err != nil {
		format.NewResponseJson(c).Err(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	var out []model.VideoIResp
	var m []tables.MarginVideo
	d := variable.DB.MySQL.Model(&m).Where("type = ?", t.Typ).Find(&m)
	if d.Error != nil {
		format.NewResponseJson(c).SuccessObj(out)
		return
	}
	for _, v := range m {
		out = append(out, model.VideoIResp{Text: v.Text, Video: v.Video})
	}
	format.NewResponseJson(c).SuccessObj(out)
}

// 获取面试数据
func GetInterviewQuestion(c *gin.Context) {
	var gsqr model.InterviewQuestionReq
	if err := bind.ShouldBinding(c, &gsqr); err != nil {
		format.NewResponseJson(c).Err(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	client := new(interview_service.Client)
	var vr = interview_service.VideoResult{Type: "init"}
	if gsqr.Type == "first" {
		vr = client.GetInterviewFirstQuestion(c, gsqr)
	} else if gsqr.Type == "next" {
		vr = client.GetInterviewNextQuestion(c, gsqr)
	} else {
		format.NewResponseJson(c).Err(30002, "`type` validate is error")
		return
	}
	if vr.Type == "init" {
		format.NewResponseJson(c).ErrObj(500, "", struct{}{})
		return
	}
	if vr.Type == "error" {
		format.NewResponseJson(c).ErrObj(500, "", struct{}{})
		return
	}
	format.NewResponseJson(c).SuccessObj(vr)
}

// 回答完成获取下一题
func FinishedReplying(c *gin.Context) {
	var gsqr model.InterviewReplyReq
	if err := bind.ShouldBinding(c, &gsqr); err != nil {
		format.NewResponseJson(c).Err(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	vr := new(interview_service.Client).FinishedReplying(c, gsqr)
	if vr.Type == "error" {
		fmt.Println(vr)
		format.NewResponseJson(c).ErrObj(500, "", struct{}{})
		return
	}
	format.NewResponseJson(c).SuccessObj(vr)
}

// 面试完成
func InterviewFinished(ctx *gin.Context) {
	var sr services.Sri
	if err := bind.ShouldBinding(ctx, &sr); err != nil {
		format.NewResponseJson(ctx).ErrObj(200, err.Error())
		return
	}
	sid := strconv.Itoa(sr.SessionId)
	if err := report_service.GenerateReport(ctx, sid, true); err != nil {
		format.NewResponseJson(ctx).ErrObj(200, err.Error())
		return
	}
	format.NewResponseJson(ctx).SuccessObj(struct{}{})
}

// 获取报告信息
func InterviewGetReport(ctx *gin.Context) {
	var sr services.Sri
	if err := bind.ShouldBinding(ctx, &sr); err != nil {
		format.NewResponseJson(ctx).ErrObj(200, err.Error())
		return
	}
	session, err := services.FindSessionById(sr.SessionId)
	if err != nil {
		format.NewResponseJson(ctx).SuccessObj(struct{}{})
		return
	}
	if session.Status != 2 && session.Url == "" {
		format.NewResponseJson(ctx).SuccessObj(struct{}{})
		return
	} else if session.Url != "" {
		format.NewResponseJson(ctx).SuccessObj(struct {
			SessionId uint   `json:"session_id"`
			CareerId  uint   `json:"career_id"`
			Url       string `json:"url"`
		}{
			SessionId: session.ID,
			CareerId:  session.CareerID,
			Url:       variable.Config.UfsInfo.UfsUrl + variable.Config.UfsInfo.BucketPrefix + session.Url,
		})
		return
	}
	format.NewResponseJson(ctx).SuccessObj(struct{}{})
}

// 生成题库视频接口
func GenerateVideo(ctx *gin.Context) {
	var hg model.HbGenerateVideo
	if err := bind.ShouldBinding(ctx, &hg); err != nil {
		format.NewResponseJson(ctx).ErrObj(200, err.Error())
		return
	}
	id, err := new(tiku_service.TikuService).HbGenerateVideo(ctx, hg)
	if err != nil {
		format.NewResponseJson(ctx).ErrObj(20001, err.Err.Error())
		return
	}
	format.NewResponseJson(ctx).SuccessObj(struct {
		Gid uint `json:"gid"`
	}{Gid: id})
}

// 获取视频
func GetVideoUrl(ctx *gin.Context) {
	var hg model.HbGetGenerateVideo
	if err := bind.ShouldBinding(ctx, &hg); err != nil {
		format.NewResponseJson(ctx).ErrObj(200, err.Error())
		return
	}
	out, err := new(tiku_service.TikuService).GetGenerationVideo(ctx, hg)
	if err != nil {
		format.NewResponseJson(ctx).ErrObj(20001, err.Err.Error())
		return
	}
	format.NewResponseJson(ctx).SuccessObj(out)
}

// 自定义职业 创建考试场次
func CreateCustomCreate(ctx *gin.Context) {
	var cc model.HbCustomCreate

	if err := bind.ShouldBinding(ctx, &cc); err != nil {
		format.NewResponseJson(ctx).ErrObj(200, err.Error())
		return
	}
	out, err := new(tiku_service.TikuService).CreateCustomCreate(ctx, cc)
	if err != nil {
		format.NewResponseJson(ctx).ErrObj(20001, err.Error())
		return
	}
	format.NewResponseJson(ctx).SuccessObj(struct {
		SessionId uint `json:"session_id"`
	}{SessionId: out})
}

// 自定义职业  根据回答结束提交接口
func CustomFinishReplay(ctx *gin.Context) {
	var cc model.HbCustomFinishReplay
	if err := bind.ShouldBinding(ctx, &cc); err != nil {
		format.NewResponseJson(ctx).ErrObj(200, err.Error())
		return
	}
	out := new(interview_service.Client).CustomFinishedReplying(ctx, cc)
	if out != nil {
		format.NewResponseJson(ctx).ErrObj(20001, out.Error())
		return
	}
	format.NewResponseJson(ctx).SuccessObj(struct{}{})
}

// 自定义职业  面试完成
func CustimInterviewFinish(ctx *gin.Context) {
	var sr services.Sri
	if err := bind.ShouldBinding(ctx, &sr); err != nil {
		format.NewResponseJson(ctx).ErrObj(200, err.Error())
		return
	}
	var session tables.Session
	d := variable.DB.MySQL.Model(&tables.Session{}).Where("id = ?", sr.SessionId).Find(&session)
	if d.Error != nil {
		format.NewResponseJson(ctx).ErrObj(200, "操作错误")
		return
	}
	uid, exists := ctx.Get("uid")
	if !exists {
		format.NewResponseJson(ctx).ErrObj(200, "您无权操作")
		return
	}
	if uid.(uint) != session.UserID {
		format.NewResponseJson(ctx).ErrObj(200, "您无权操作该场面试~")
		return
	}

	sid := strconv.Itoa(sr.SessionId)
	// 面试完成更新数据
	variable.DB.MySQL.Model(&tables.Session{}).Where("id = ?", sid).Update("finish_at", time.Now())

	if err := report_service.GenerateReport(ctx, sid, true); err != nil {
		format.NewResponseJson(ctx).ErrObj(200, err.Error())
		return
	}
	format.NewResponseJson(ctx).SuccessObj(struct{}{})
}

// 获取报告信息
func CustomInterviewGetReport(ctx *gin.Context) {
	var sr services.Sri
	if err := bind.ShouldBinding(ctx, &sr); err != nil {
		format.NewResponseJson(ctx).ErrObj(200, err.Error())
		return
	}
	session, err := services.FindSessionById(sr.SessionId)
	if err != nil {
		format.NewResponseJson(ctx).SuccessObj(struct{}{})
		return
	}
	if session.Status != 2 && session.Url == "" {
		format.NewResponseJson(ctx).SuccessObj(struct{}{})
		return
	} else if session.Url != "" {
		format.NewResponseJson(ctx).SuccessObj(struct {
			SessionId uint   `json:"session_id"`
			CareerId  uint   `json:"career_id"`
			Url       string `json:"url"`
		}{
			SessionId: session.ID,
			CareerId:  session.CareerID,
			Url:       variable.Config.UfsInfo.UfsUrl + variable.Config.UfsInfo.BucketPrefix + session.Url,
		})
		return
	}
	format.NewResponseJson(ctx).SuccessObj(struct{}{})
}
